var express = require('express');
var router = express.Router();
var base = '/course/type/';

var reqUtil = require('../lib/req');
var LINK = require('../api/link');


router.get('/', function(req, res) {

    var page = parseInt(req.query.page) || 1;
    reqUtil.getData(LINK.REQ_GET_COURSE_TYPE, function(code, level) {
        console.log(code, level);
        if (!code) {
            var second = [];
            for (var i in level) {
                var item = level[i];
                var url = base + item.key + '/0/0';
                second.push({ key: item.key, label: item.label, active: item.active, url: url });
            }
            reqUtil.getData(LINK.REQ_GET_COURSE_LIST, function(code, data) {
                if (!code) {
                    var user = false;
                    if (req.session) {
                        if (req.session.user) {
                            user = true;
                        }
                    };
                    var obj = {
                        user: user,
                        nav: true,
                        second: second,
                        list: data.page_data,
                        spread: data.spread,
                        "page_now": data.page_now,
                        "total_page": data.total_page,
                        "total_rows": data.total_rows,
                        url: req.baseUrl + req.path
                    };
                    res.render('course/list', obj);
                } else {
                    res.redirect('/');
                }
            });

        } else {
            res.redirect('/');
        }
    });

});

router.get('/type/:level0/:level1/:level2', function(req, res) {
    console.log(req.originalUrl);
    var type0 = req.params.level0;
    var type1 = req.params.level1;
    var type2 = req.params.level2;
    var level0, level1, level2, active, second;
    var page = parseInt(req.query.page) || 1;
    var l, k;
    reqUtil.getData(LINK.REQ_GET_COURSE_TYPE, function(code, level) {
        if (!code) {
            level0 = [];
            active = [];
            second = [];
            for (var i in level) {
                var item = level[i];
                if (type0 > 0) {
                    l = 1;
                    if (item.key == type0) {
                        item.active = true;
                        //设置选中
                        k = item.key;
                        active.push({ key: item.key, label: item.label });
                        level1 = [];
                        var children = item.children;

                        for (var j in children) {
                            var node = children[j];
                            if (type1 > 0) {
                                l = 2;
                                if (node.key == type1) {
                                    //设置选中
                                    k = node.key;
                                    node.active = true;
                                    active.push({ key: node.key, label: node.label });
                                    level2 = [];
                                    var child = node.children;
                                    for (var l in child) {
                                        var leaf = child[l];
                                        if (type2 > 0) {
                                            l = 3;
                                            if (leaf.key == type2) {
                                                //设置选中
                                                k = leaf.key;
                                                leaf.active = true;
                                                active.push({ key: leaf.key, label: leaf.label });
                                            }
                                        } else {
                                            second = level2;
                                        }
                                        var url = base + item.key + '/' + node.key + '/' + leaf.key;
                                        level2.push({ key: leaf.key, label: leaf.label, active: leaf.active, url: url });
                                    }
                                }
                            } else {
                                second = level1;
                            }
                            var url = base + item.key + '/' + node.key + '/0';
                            level1.push({ key: node.key, label: node.label, active: node.active, url: url });
                        }
                    }

                } else {
                    second = level0;
                }
                //设置level0
                var url = base + item.key + '/0/0';
                level0.push({ key: item.key, label: item.label, active: item.active, url: url });
            }

            var link = LINK.REQ_GET_COURSE_LIST;
            if (l > 0 && k > 0) {
                link += '&l=' + l + '&k=' + k;
            }
            console.log(l, k, link);
            reqUtil.getData(link, function(code, data) {
                if (!code) {
                    var user = false;
                    if (req.session) {
                        if (req.session.user) {
                            user = true;
                        }
                    };
                    var obj = {
                        nav: true,
                        user: user,
                        active: active,
                        level: { 0: level0, 1: level1, 2: level2 },
                        second: second,
                        list: data.page_data,
                        spread: data.spread,
                        "page_now": data.page_now,
                        "total_page": data.total_page,
                        "total_rows": data.total_rows,
                        url: req.baseUrl + req.path
                    };
                    res.render('course/list', obj);
                } else {
                    res.redirect('/');
                }
            });
        } else {
            res.redirect('/');
        }
    });

});

router.get('/detail/:id', function(req, res) {
    var id = req.params.id;
    reqUtil.getData(LINK.REQ_GET_COURSE_DETAIL + id, function(code, data) {
        if (!code) {
            var user = false;
            if (req.session) {
                if (req.session.user) {
                    user = true;
                }
            };
            data.nav = true;
            var detail = data.detail;
            console.dir(data);
            var obj = {
                user: user,
                nav: true,
                type: data.type,
                brief: detail.brief,
                chapter: detail.chapter,
                spread: detail.spread,
                content: detail.content
            };
            res.render('course/content', obj);
        } else {
            res.redirect('/');
        }
    });
});

module.exports = router;